/******************************************************************
 * 文件：EXTI.c
 * 功能：实现外部中断相关函数
 * 日期：2018-02-25
 * 作者：zx
 * 版本：Ver.1.0 | 最初版本
 * 
 * Copyright (C) 2018 zx. All rights reserved.
*******************************************************************/
#include "EXTI/EXTI.h"

int EXTI_Value[10] = {0}; //Final_Value 上位机监测数值的对比
double Final_Value=0;
u8 presscnt_Baisic=0,  presscnt_DIF=0, Enable_flag=0;
u8 Start_Num=0;			//学习起点
int Stage_sta=0,Test_Num=0;
u32 B_D_sum=0,Sheet_Num=0;
int Tested[100] = {0};//用于存放已测量的数据
int Per_num[100] = {0};//用于存放纸张间隔

	double parameterA;
	double parameterB;




//定义结构体变量
Pre_Test Is_Test;

/**
 * 功能：蜂鸣器蜂鸣
 * 参数：times:鸣响次数
 * 		 MS:鸣响时长 ms
 * 返回值：None
 */
void Buzzing(u8 times, u16 MS)
{
	u8 i=0;
	for(i=0; i<times; i++)
	{
		GPIO_ResetBits(GPIOA, RELAY1);
		Delay_ms(MS);
		GPIO_SetBits(GPIOA, RELAY1);
		Delay_ms(MS);
	}
	
}



/**
 * 功能：测量参数初始化
 * 参数：None
 * 返回值：None
 */
void Test_Init()
{
	
  	Is_Test.parameterA_0_9=0;
    Is_Test.parameterB_0_9=0;

    Is_Test.parameterA_10_13=0;
    Is_Test.parameterB_10_13=0;

    Is_Test.parameterA_14_23=0;
    Is_Test.parameterB_14_23=0;

    Is_Test.parameterA_24_31=0;
    Is_Test.parameterB_24_31=0;

    Is_Test.parameterA_32_34=0;
    Is_Test.parameterB_32_34=0;

	
}


/**
 * 功能：测量参数
 * 参数：None
 * 返回值：平均值
 */
u32 Testing()
{
		u32 sum=0;
		u32 Distance=0;
		u8 Test_Num=0;

		//formatScreen(0X00);//清屏
		//showString(0,2,"Measuring...",FONT_16_EN);
		for(Test_Num=0;Test_Num<10;Test_Num++)
		{
			EXTI_Value[Test_Num] = FDC2X14ReadCH(3);
			//blinkLED(1,10,10); //延时待定
			
		}
		quickSort(EXTI_Value,0,9);
		for(Test_Num=1;Test_Num<9;Test_Num++)
		{
			sum+=EXTI_Value[Test_Num];
		}

		Final_Value=(sum/8.0/10000.0);
		sum=0;
		Distance=(u32)((Final_Value)*(Final_Value)+0.5);

		return Distance;
}

/**
 * 功能：初始化外部中断3
 * 参数：None
 * 返回值：None
 */
void initEXTI_0_1_2(void)
{
  
    EXTI_InitTypeDef EXTI_InitStructure;                        //定义外部中断初始化结构体

    RCC_APB2PeriphClockCmd(RCC_APB2Periph_AFIO, ENABLE);        //开启外设复用时钟

    GPIO_EXTILineConfig(GPIO_PortSourceGPIOA, GPIO_PinSource0); //使用PA0作为EXTI0信号输入源
    EXTI_InitStructure.EXTI_Line = EXTI_Line0;                  //设置外部中断线0
    EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;         //外部中断模式
    EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Falling;     //下降沿触发中断
    EXTI_InitStructure.EXTI_LineCmd = ENABLE;                   //使能外部中断屏蔽寄存器
    EXTI_Init(&EXTI_InitStructure);                             //设置生效


	GPIO_EXTILineConfig(GPIO_PortSourceGPIOA, GPIO_PinSource1); //使用PA1作为EXTI1信号输入源
    EXTI_InitStructure.EXTI_Line = EXTI_Line1;                  //设置外部中断线1
    EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;         //外部中断模式
    EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Falling;     //下降沿触发中断
    EXTI_InitStructure.EXTI_LineCmd = ENABLE;                   //使能外部中断屏蔽寄存器
    EXTI_Init(&EXTI_InitStructure);                             //设置生效


	GPIO_EXTILineConfig(GPIO_PortSourceGPIOA, GPIO_PinSource2); //使用PA1作为EXTI1信号输入源
    EXTI_InitStructure.EXTI_Line = EXTI_Line2;                  //设置外部中断线2
    EXTI_InitStructure.EXTI_Mode = EXTI_Mode_Interrupt;         //外部中断模式
    EXTI_InitStructure.EXTI_Trigger = EXTI_Trigger_Falling;     //下降沿触发中断
    EXTI_InitStructure.EXTI_LineCmd = ENABLE;                   //使能外部中断屏蔽寄存器
    EXTI_Init(&EXTI_InitStructure);                             //设置生效

}


/**
 * 功能：外部中断3中断服务函数
 * 参数：None
 * 返回值：None
 */

void EXTI0_IRQHandler(void)
{
//	u8 n=0;
	EXTI_GetFlagStatus(EXTI_Line0);    //获取外部中断挂起位
	Sheet_Num=0;
	u32 Current_Value=0;
  

	if(getKeyValue(KEY_PRESS)==KEY_UP)
	{
		
		Current_Value = Testing();
		if(Current_Value > MAX)
		{
			formatScreen(0x00);//清屏
			showString(0,1,"Error:",FONT_16_EN);
			showString(0,3,"short circuit!",FONT_16_EN);
			Buzzing(10, 50);
			return;
		}
		else
		{
			Get_Params();
			Sheet_Num = Get_sheet_num(Current_Value);
			formatScreen(0x00);//清屏
			showString(0,0,"The Num is:",FONT_16_EN);
			showNumber(40,2,Sheet_Num,DEC,3,FONT_16_EN);
			showString(0,4,"open circuit",FONT_16_EN);
			
			Buzzing(1, 400);
		}
	
	
		//NVIC_SystemReset();                 //重启单片机
	}  
	EXTI_ClearFlag(EXTI_Line0);        //清除外部中断挂起位
	
}
    
    




/**
 * 功能：外部中断1中断服务函数
 * 参数：None
 * 返回值：None
 */
void EXTI1_IRQHandler(void)
{
	
    EXTI_GetFlagStatus(EXTI_Line1);    //获取外部中断挂起位
   
	if(getKeyValue(KEY_RELEASE)==KEY_DOWN)
	{
		presscnt_Baisic++;
		

		if(presscnt_Baisic==1)
		Test_Init(); 					   //仅在开始测量时进行参数初始化

		switch(presscnt_Baisic)
		{
			case 1:
					Tested[0]=Testing();
					//Per_num[0]=Start_Num;
					Per_num[0]=1;
					formatScreen(0x00);//清屏
					showString(0,0,"Basic_1",FONT_16_EN);
					showNumber(40,2,Tested[0],DEC,3,FONT_16_EN);
					showString(0,4,"stage-1",FONT_16_EN);
					Buzzing(1,80);
					break;

			case 2:
					Tested[1]=Testing();
					Per_num[1]=Is_Test.interval + Per_num[0];
					formatScreen(0x00);//清屏
					showString(0,0,"Basic_2",FONT_16_EN);
					showNumber(40,2,Tested[1],DEC,3,FONT_16_EN);
					showString(0,4,"stage-1",FONT_16_EN);
					Buzzing(1,80);
					break;


			case 3:
					Tested[2]=Testing();
					Per_num[2]=2*Is_Test.interval + Per_num[0];
					formatScreen(0x00);//清屏
					showString(0,0,"Basic_3",FONT_16_EN);
					showNumber(40,2,Tested[2],DEC,3,FONT_16_EN);
					showString(0,4,"stage-1",FONT_16_EN);
					Buzzing(1,80);
					break;


			case 4:
					Tested[3]=Testing();
					Per_num[3]=3*Is_Test.interval + Per_num[0];
					formatScreen(0x00);//清屏
					showString(0,0,"Basic_4",FONT_16_EN);
					showNumber(40,2,Tested[3],DEC,3,FONT_16_EN);
					showString(0,4,"stage-1",FONT_16_EN);
					Buzzing(1,80);
					break;


			case 5:
					Tested[4]=Testing();
					Per_num[4]=4*Is_Test.interval + Per_num[0];
					formatScreen(0x00);//清屏
					showString(0,0,"Basic_5",FONT_16_EN);
					showNumber(40,2,Tested[4],DEC,3,FONT_16_EN);
					showString(0,4,"stage-1",FONT_16_EN);
					Buzzing(1,80);
					break;


			case 6:
			
					Tested[5]=Testing();
					Per_num[5]=5*Is_Test.interval + Per_num[0];
					formatScreen(0x00);//清屏
					showString(0,0,"Basic_6",FONT_16_EN);
					showNumber(40,2,Tested[5],DEC,3,FONT_16_EN);
					showString(0,4,"stage-1",FONT_16_EN);
					Buzzing(1,80);
					break;


			case 7:
			
					Tested[6]=Testing();
					Per_num[6]=6*Is_Test.interval + Per_num[0];
					formatScreen(0x00);//清屏
					showString(0,0,"Basic_7",FONT_16_EN);
					showNumber(40,2,Tested[6],DEC,3,FONT_16_EN);
					showString(0,4,"stage-1",FONT_16_EN);
					Buzzing(1,80);
					break;


			case 8:
			
					Tested[7]=Testing();
					Per_num[7]=7*Is_Test.interval + Per_num[0];
					formatScreen(0x00);//清屏
					showString(0,0,"Basic_8",FONT_16_EN);
					showNumber(40,2,Tested[7],DEC,3,FONT_16_EN);
					showString(0,4,"stage-1",FONT_16_EN);
					Buzzing(1,80);
					break;


			case 9:
			
					Tested[8]=Testing();
					Per_num[8]=8*Is_Test.interval + Per_num[0];
					formatScreen(0x00);//清屏
					showString(0,0,"Basic_9",FONT_16_EN);
					showNumber(40,2,Tested[8],DEC,3,FONT_16_EN);
					showString(0,4,"stage-1",FONT_16_EN);
					Buzzing(1,80);
					break;


			case 10:
			
					Tested[9]=Testing();
					Per_num[9]=9*Is_Test.interval + Per_num[0];
					formatScreen(0x00);//清屏
					showString(0,0,"Basic_10",FONT_16_EN);
					showNumber(40,2,Tested[9],DEC,3,FONT_16_EN);
					showString(0,4,"stage-1 is ok",FONT_16_EN);  //第一阶段学习完成
					Buzzing(3,50); //蜂鸣3声，表示basic 测量结束
					break;


			case 11:
			
					Tested[10]=Testing();
					Per_num[10]=10*Is_Test.interval + Per_num[0];
					formatScreen(0x00);//清屏
					showString(0,0,"Basic_11",FONT_16_EN);
					showNumber(40,2,Tested[10],DEC,3,FONT_16_EN);
					showString(0,4,"stage-2",FONT_16_EN);
					Buzzing(1,80);
					
					
					break;


			case 12:
			
					Tested[11]=Testing();
					Per_num[11]=11*Is_Test.interval + Per_num[0];
					formatScreen(0x00);//清屏
					showString(0,0,"Basic_12",FONT_16_EN);
					showNumber(40,2,Tested[11],DEC,3,FONT_16_EN);
					showString(0,4,"stage-2",FONT_16_EN);
					Buzzing(1,80);
					break;


			case 13:
			
					Tested[12]=Testing();
					Per_num[12]=12*Is_Test.interval + Per_num[0];
					formatScreen(0x00);//清屏
					showString(0,0,"Basic_13",FONT_16_EN);
					showNumber(40,2,Tested[12],DEC,3,FONT_16_EN);
					showString(0,4,"stage-2",FONT_16_EN);
					Buzzing(1,80);
					break;


			case 14:
			
					Tested[13]=Testing();
					Per_num[13]=13*Is_Test.interval + Per_num[0];
					formatScreen(0x00);//清屏
					showString(0,0,"Basic_14",FONT_16_EN);
					showNumber(40,2,Tested[13],DEC,3,FONT_16_EN);
					showString(0,4,"stage-2 is ok",FONT_16_EN);		
					Buzzing(3,50);
					break;


			case 15:
			
					Tested[14]=Testing();
					Per_num[14]=14*Is_Test.interval + Per_num[0];
					formatScreen(0x00);//清屏
					showString(0,0,"Basic_15",FONT_16_EN);
					showNumber(40,2,Tested[14],DEC,3,FONT_16_EN);
					showString(0,4,"stage-3",FONT_16_EN);
					Buzzing(1,80);
					break;


			case 16:
			
					Tested[15]=Testing();
					Per_num[15]=15*Is_Test.interval + Per_num[0];
					formatScreen(0x00);//清屏
					showString(0,0,"Basic_16",FONT_16_EN);
					showNumber(40,2,Tested[15],DEC,3,FONT_16_EN);
					showString(0,4,"stage-3",FONT_16_EN);	
					Buzzing(1,80);
					break;


			case 17:
			
					Tested[16]=Testing();
					Per_num[16]=16*Is_Test.interval + Per_num[0];
					formatScreen(0x00);//清屏
					showString(0,0,"Basic_17",FONT_16_EN);
					showNumber(40,2,Tested[16],DEC,3,FONT_16_EN);
					showString(0,4,"stage-3",FONT_16_EN);
					Buzzing(1,80);
					break;


			case 18:
			
					Tested[17]=Testing();
					Per_num[17]=17*Is_Test.interval + Per_num[0];
					formatScreen(0x00);//清屏
					showString(0,0,"Basic_18",FONT_16_EN);
					showNumber(40,2,Tested[17],DEC,3,FONT_16_EN);
					showString(0,4,"stage-3",FONT_16_EN);
					Buzzing(1,80); //蜂鸣3声，表示basic 测量结束
					break;


			case 19:
			
					Tested[18]=Testing();
					Per_num[18]=18*Is_Test.interval + Per_num[0];
					formatScreen(0x00);//清屏
					showString(0,0,"Basic_19",FONT_16_EN);
					showNumber(40,2,Tested[18],DEC,3,FONT_16_EN);
					showString(0,4,"stage-3",FONT_16_EN);
					Buzzing(1,80);
					break;


			case 20:
			
					Tested[19]=Testing();
					Per_num[19]=19*Is_Test.interval + Per_num[0];
					formatScreen(0x00);//清屏
					showString(0,0,"Basic_20",FONT_16_EN);
					showNumber(40,2,Tested[19],DEC,3,FONT_16_EN);
					showString(0,4,"stage-3",FONT_16_EN);
					Buzzing(1,80);
					break;


			case 21:
			
					Tested[20]=Testing();
					Per_num[20]=20*Is_Test.interval + Per_num[0];
					formatScreen(0x00);//清屏
					showString(0,0,"Basic_21",FONT_16_EN);
					showNumber(40,2,Tested[20],DEC,3,FONT_16_EN);
					showString(0,4,"stage-3",FONT_16_EN);
					Buzzing(1,80);
					break;


			case 22:
			
					Tested[21]=Testing();
					Per_num[21]=21*Is_Test.interval + Per_num[0];
					formatScreen(0x00);//清屏
					showString(0,0,"Basic_22",FONT_16_EN);
					showNumber(40,2,Tested[21],DEC,3,FONT_16_EN);
					showString(0,4,"stage-3",FONT_16_EN);
					Buzzing(1,80);
					break;


			case 23:
			
					Tested[22]=Testing();
					Per_num[22]=22*Is_Test.interval + Per_num[0];
					formatScreen(0x00);//清屏
					showString(0,0,"Basic_23",FONT_16_EN);
					showNumber(40,2,Tested[22],DEC,3,FONT_16_EN);
					showString(0,4,"stage-3",FONT_16_EN);
					Buzzing(1,80);
					break;


			case 24:
			
					Tested[23]=Testing();
					Per_num[23]=23*Is_Test.interval + Per_num[0];
					formatScreen(0x00);//清屏
					showString(0,0,"Basic_24",FONT_16_EN);
					showNumber(40,2,Tested[23],DEC,3,FONT_16_EN);
					showString(0,4,"stage-3 is ok",FONT_16_EN);
					Buzzing(3,50);
					break;


			case 25:
			
					Tested[24]=Testing();
					Per_num[24]=24*Is_Test.interval + Per_num[0];
					formatScreen(0x00);//清屏
					showString(0,0,"Basic_25",FONT_16_EN);
					showNumber(40,2,Tested[24],DEC,3,FONT_16_EN);
					showString(0,4,"stage-4",FONT_16_EN);
					Buzzing(1,80);
				
					break;


			case 26:
			
					Tested[25]=Testing();
					Per_num[25]=25*Is_Test.interval + Per_num[0];
					formatScreen(0x00);//清屏
					showString(0,0,"Basic_26",FONT_16_EN);
					showNumber(40,2,Tested[25],DEC,3,FONT_16_EN);
					showString(0,4,"stage-4",FONT_16_EN);
					Buzzing(1,80);
					break;


			case 27:
			
					Tested[26]=Testing();
					Per_num[26]=26*Is_Test.interval + Per_num[0];
					formatScreen(0x00);//清屏
					showString(0,0,"Basic_27",FONT_16_EN);
					showNumber(40,2,Tested[26],DEC,3,FONT_16_EN);
					showString(0,4,"stage-4",FONT_16_EN);
					Buzzing(1,80);
					break;


			case 28:
			
					Tested[27]=Testing();
					Per_num[27]=27*Is_Test.interval + Per_num[0];
					formatScreen(0x00);//清屏
					showString(0,0,"Basic_28",FONT_16_EN);
					showNumber(40,2,Tested[27],DEC,3,FONT_16_EN);
					showString(0,4,"stage-4",FONT_16_EN);
					Buzzing(1,80);
					break;


			case 29:
			
					Tested[28]=Testing();
					Per_num[28]=28*Is_Test.interval + Per_num[0];
					formatScreen(0x00);//清屏
					showString(0,0,"Basic_29",FONT_16_EN);
					showNumber(40,2,Tested[28],DEC,3,FONT_16_EN);
					showString(0,4,"stage-4",FONT_16_EN);
					Buzzing(1,80);
					break;


			case 30:
			
					Tested[29]=Testing();
					Per_num[29]=29*Is_Test.interval + Per_num[0];
					formatScreen(0x00);//清屏
					showString(0,0,"Basic_30",FONT_16_EN);
					showNumber(40,2,Tested[29],DEC,3,FONT_16_EN);
					showString(0,4,"stage-4",FONT_16_EN);
				

					Buzzing(1,80); //蜂鸣3声，表示basic 测量结束
					

					break;


			case 31:
			
					Tested[30]=Testing();
					Per_num[30]=30*Is_Test.interval + Per_num[0];
					formatScreen(0x00);//清屏
					showString(0,0,"Basic_31",FONT_16_EN);
					showNumber(40,2,Tested[30],DEC,3,FONT_16_EN);
					showString(0,4,"stage-4",FONT_16_EN);
					Buzzing(1,80);
					break;


			case 32:
			
					Tested[31]=Testing();
					Per_num[31]=31*Is_Test.interval + Per_num[0];
					formatScreen(0x00);//清屏
					showString(0,0,"Basic_32",FONT_16_EN);
					showNumber(40,2,Tested[31],DEC,3,FONT_16_EN);
					showString(0,4,"stage-4 is ok",FONT_16_EN);
					Buzzing(3,50);
					break;


			case 33:
			
					Tested[32]=Testing();
					Per_num[32]=32*Is_Test.interval + Per_num[0];
					formatScreen(0x00);//清屏
					showString(0,0,"Basic_33",FONT_16_EN);
					showNumber(40,2,Tested[32],DEC,3,FONT_16_EN);
					showString(0,4,"stage-5",FONT_16_EN);
					Buzzing(1,80); //蜂鸣3声，表示basic 测量结束
				
					break;


			case 34:
			
					Tested[33]=Testing();
					Per_num[33]=33*Is_Test.interval + Per_num[0];
					formatScreen(0x00);//清屏
					showString(0,0,"Basic_34",FONT_16_EN);
					showNumber(40,2,Tested[33],DEC,3,FONT_16_EN);
					showString(0,4,"stage-5",FONT_16_EN);
					Buzzing(1,80);
					break;


			case 35:
			
					Tested[34]=Testing();
					Per_num[34]=34*Is_Test.interval + Per_num[0];
					formatScreen(0x00);//清屏
					showString(0,0,"Basic_35",FONT_16_EN);
					showNumber(40,2,Tested[34],DEC,3,FONT_16_EN);
					showString(0,4,"stage-5 is ok",FONT_16_EN);
					Buzzing(4,80);
					break;


			// case 36:
			
			// 		Tested[35]=Testing();
			// 		Per_num[35]=35*Is_Test.interval + Per_num[0];
			// 		formatScreen(0x00);//清屏
			// 		showString(0,0,"Basic_36",FONT_16_EN);
			// 		showNumber(40,2,Tested[35],DEC,3,FONT_16_EN);
			// 		showString(0,4,"stage-6 is ok",FONT_16_EN);
			// 		Buzzing(3,50);
			// 		break;


			// case 37:
			
			// 		Tested[36]=Testing();
			// 		Per_num[36]=28*Is_Test.interval + Per_num[0];
			// 		formatScreen(0x00);//清屏
			// 		showString(0,0,"Basic_37",FONT_16_EN);
			// 		showNumber(40,2,Tested[36],DEC,3,FONT_16_EN);
			// 		showString(0,4,"stage-7",FONT_16_EN);
			// 		Buzzing(1,80);
			// 		break;


			// case 38:
			
			// 		Tested[37]=Testing();
			// 		Per_num[37]=37*Is_Test.interval + Per_num[0];
			// 		formatScreen(0x00);//清屏
			// 		showString(0,0,"Basic_38",FONT_16_EN);
			// 		showNumber(40,2,Tested[37],DEC,3,FONT_16_EN);
			// 		showString(0,4,"stage_7",FONT_16_EN);
			// 		Buzzing(1,80);
			// 		break;


			// case 39:
			
			// 		Tested[38]=Testing();
			// 		Per_num[38]=38*Is_Test.interval + Per_num[0];
			// 		formatScreen(0x00);//清屏
			// 		showString(0,0,"Basic_39",FONT_16_EN);
			// 		showNumber(40,2,Tested[38],DEC,3,FONT_16_EN);
			// 		showString(0,4,"stage-7",FONT_16_EN);
			// 		Buzzing(1,80);
			// 		break;


			// case 40:
			
			// 		Tested[39]=Testing();
			// 		Per_num[39]=39*Is_Test.interval + Per_num[0];
			// 		formatScreen(0x00);//清屏
			// 		showString(0,0,"Basic_40",FONT_16_EN);
			// 		showNumber(40,2,Tested[39],DEC,3,FONT_16_EN);
			// 		showString(0,4,"stage-7",FONT_16_EN);
			// 		Buzzing(1,80);
			// 		break;


			// case 41:
			
			// 		Tested[40]=Testing();
			// 		Per_num[40]=40*Is_Test.interval + Per_num[0];
			// 		formatScreen(0x00);//清屏
			// 		showString(0,0,"Basic_41",FONT_16_EN);
			// 		showNumber(40,2,Tested[40],DEC,3,FONT_16_EN);
			// 		showString(0,4,"stage-7",FONT_16_EN);
			// 		Buzzing(1,80);
			// 		break;


			// case 42:
			
			// 		Tested[41]=Testing();
			// 		Per_num[41]=41*Is_Test.interval + Per_num[0];
			// 		formatScreen(0x00);//清屏
			// 		showString(0,0,"Basic_42",FONT_16_EN);
			// 		showNumber(40,2,Tested[41],DEC,3,FONT_16_EN);
			// 		showString(0,4,"stage-7",FONT_16_EN);
			// 		Buzzing(1,80);
			// 		break;
					

		
			default:                        break;

		}
		

		
		


	}
	
	 EXTI_ClearFlag(EXTI_Line1);        //清除外部中断挂起位
}




/**
 * 功能：外部中断2中断服务函数
 * 参数：None
 * 返回值：None
 */
void EXTI2_IRQHandler(void)
{
    EXTI_GetFlagStatus(EXTI_Line2);    //获取外部中断挂起位
    EXTI_ClearFlag(EXTI_Line2);        //清除外部中断挂起位
	if(getKeyValue(KEY_RELEASE)==KEY_Three)
	{
		
		// presscnt_DIF++;
		// Is_Test.interval=presscnt_DIF;
		
		//  if(presscnt_DIF==7)
		// {
		// 	presscnt_DIF=1;
		// 	Is_Test.interval=presscnt_DIF;
		// }
		
		
		// formatScreen(0x00);//清屏
		// showString(0,0,"interval",FONT_16_EN);
		// showNumber(60,3,Is_Test.interval,DEC,3,FONT_16_EN);
		// Buzzing(1,100);//长鸣一声，表示  差值  计算结束
		//Enable_flag=1;

		
		
	}
		
		// switch(presscnt_DIF)
		// {
		// 	case 1:
		// 			Is_Test.DIF_1=Testing();
		// 			formatScreen(0x00);//清屏
		// 			showString(0,0,"DIF_1",FONT_16_EN);
		// 			showNumber(40,2,Is_Test.DIF_1,DEC,3,FONT_16_EN);
		// 			break;

		// 	case 2:
		// 			Is_Test.DIF_2=Testing();
		// 			formatScreen(0x00);//清屏
		// 			showString(0,0,"DIF_2",FONT_16_EN);
		// 			showNumber(40,2,Is_Test.DIF_2,DEC,3,FONT_16_EN);
		// 			break;


		// 	case 3:
		// 			Is_Test.DIF_3=Testing();
		// 			formatScreen(0x00);//清屏
		// 			showString(0,0,"DIF_3",FONT_16_EN);
		// 			showNumber(40,2,Is_Test.DIF_3,DEC,3,FONT_16_EN);
		// 			break;


		// 	case 4:
		// 			Is_Test.DIF_4=Testing();
		// 			formatScreen(0x00);//清屏
		// 			showString(0,0,"DIF_4",FONT_16_EN);
		// 			showNumber(40,2,Is_Test.DIF_4,DEC,3,FONT_16_EN);
		// 			break;


		// 	case 5:
					
		// 			Is_Test.DIF_5=Testing();
		// 			formatScreen(0x00);//清屏
		// 			showString(0,0,"DIF_5",FONT_16_EN);
		// 			showNumber(40,2,Is_Test.DIF_5,DEC,3,FONT_16_EN);
		// 			Delay_ms(1000);
		// 			formatScreen(0x00);//清屏
		// 			showString(0,0,"DIF OK!",FONT_16_EN);
					
		// 			Buzzing(3,400); //蜂鸣3声，表示basic 测量结束
					
		// 			break;
		// 	default:                        break;

		// }

	


	
}





/**
 功能：快速排列
 * 参数：a:数组名
 *      left:数组起始下标
 *      right:数组end下标
 * 返回值：None
 */
void quickSort(int *a,int left,int right)
{
	int i=left;
	int j=right;
	double temp=a[left];
	if(left>=right)
		return;
	while(i!=j)
	{
		while(i<j&&a[j]>=temp)
		j--;
		if(j>i)
			a[i]=a[j];//a[i]已经赋值给temp,所以直接将a[j]赋值给a[i],赋值完之后a[j],有空位
		while(i<j&&a[i]<=temp)
		i++;
		if(i<j)
		a[j]=a[i];
	}
	a[i]=temp;//把基准插入,此时i与j已经相等R[low..pivotpos-1].keys≤R[pivotpos].key≤R[pivotpos+1..high].keys
	quickSort(a,left,i-1);/*递归左边*/
	quickSort(a,i+1,right);/*递归右边*/
}



/**
 功能：曲线拟合
 * 参数：DataPointer:数组名
 *      Per:纸张间隔
 *      
 * 返回值：纸张张数
 */

/* 利用最小二乘法测量k,b 并返回当前张数 */
// u32 createPoint(u32 *DataPointer, u32 Per, u32 Y)
// {

// 	tmp = 0;
// 	sumX=0;//第一张
// 	sumY=0;
// 	averageX = 0;
// 	averageY = 0;
// 	sumUp = 0;
// 	sumDown = 0;
	
	
	// if(type == FIND_LEFT)
	// 	DataPointer = &leftLine[line];
	// else if(type == FIND_RIGHT)
	// 	DataPointer = &rightLine[line];
	// else
	// 	DataPointer = &centerLine[line];
	
	// 取邻近的 POINT_COUNT 个点进行拟合

	
	// for(tmp=1; tmp<6; tmp++ )
	// {
	// 	sumX += Per;
	// 	sumY += DataPointer[tmp];
	// }
	
	// averageX = sumX/tmp;
	// averageY = sumY/tmp;
	// do
	// {
	// 	sumUp += (DataPointer[tmp]-averageY) * (tmp-averageX);
	// 	sumDown += (tmp-averageX) * (tmp-averageX);
	// } while(tmp-- > 0);
	
	
	// if(sumDown == 0)
	// 	parameterB = 0;
	// else
	// 	parameterB = sumUp/sumDown;

// 	parameterB = sumY/sumX;
// 	parameterA = averageY-parameterB*averageX;
// 	return (u32)((Y-parameterA)/parameterB + 0.5);
// }


//-------------------------------------------------------------
//功能 : 最小二乘法直线拟合 y = a·x + b， 计算系数a 和 b
//参数 : x -- 辐照度的数组
//       y --  功率的数组
//       Stage_start 阶段起始测量纸张数
//       num 是数组包含的元素个数，x[]和y[]的元素个数必须相等
//       a,b 都是返回值
//       Y 当前传感器的测量值
//返回 : 纸张张数
//-------------------------------------------------------------
// int leastSquareLinearFit(int x[], int y[] ,int Stage_start, int num, int Y)
// {
// 	int i = 0;
//     int sum_x2 = 0.0;
//     int sum_y  = 0.0;
//     int sum_x  = 0.0;
//     int sum_xy = 0.0;

  
//         for (i = Stage_start; i < num; ++i) {
//             sum_x2 += x[i]*(u32)x[i];
//             sum_y  += y[i];
//             sum_x  += x[i];
//             sum_xy += x[i]*y[i];
//         }

//     parameterA = ((num-Stage_start)*sum_xy - sum_x*sum_y)/(double)((num-Stage_start)*sum_x2 - sum_x*sum_x);
//     parameterB = ((sum_x2*sum_y - sum_x*sum_xy)/(double)((num-Stage_start)*sum_x2-sum_x*sum_x));
// 	return (int)((Y-parameterB)/parameterA + 0.5);


   
// }
  



void leastSquareLinearFit(int x[], int y[] ,int Stage_start, int num)
{
	int i = 0;
    int sum_x2 = 0.0;
    int sum_y  = 0.0;
    int sum_x  = 0.0;
    int sum_xy = 0.0;

  
        for (i = Stage_start; i <= num; ++i) {
            sum_x2 += x[i]*(u32)x[i];
            sum_y  += y[i];
            sum_x  += x[i];
            sum_xy += x[i]*y[i];
        }

    parameterA = ((num-Stage_start+1)*sum_xy - sum_x*sum_y)/(double)((num-Stage_start+1)*sum_x2 - sum_x*sum_x);
    parameterB = ((sum_x2*sum_y - sum_x*sum_xy)/(double)((num-Stage_start+1)*sum_x2-sum_x*sum_x));



   
}  

void Get_Params()
{
	//leastSquareLinearFit(int x[], int y[] ,int Stage_start, int num)
	leastSquareLinearFit(Per_num, Tested, 0, 9);
	Is_Test.parameterA_0_9 = parameterA;
	Is_Test.parameterB_0_9 = parameterB;

	leastSquareLinearFit(Per_num, Tested, 10, 13);
	Is_Test.parameterA_10_13 = parameterA;
	Is_Test.parameterB_10_13 = parameterB;

	leastSquareLinearFit(Per_num, Tested, 14, 23);
	Is_Test.parameterA_14_23 = parameterA;
	Is_Test.parameterB_14_23 = parameterB;

	leastSquareLinearFit(Per_num, Tested, 24, 31);
	Is_Test.parameterA_24_31 = parameterA;
	Is_Test.parameterB_24_31 = parameterB;

	leastSquareLinearFit(Per_num, Tested, 32, 34);
	Is_Test.parameterA_32_34 = parameterA;
	Is_Test.parameterB_32_34 = parameterB;

	


}



u32 Get_sheet_num(int Y)
{
	Is_Test._1_10=0;
	Is_Test._11_14=0;
	Is_Test._15_24=0;
	Is_Test._25_32=0;
	Is_Test._33_35=0;


	u32 Number=0;
	Number = (int)((Y-Is_Test.parameterB_0_9)/Is_Test.parameterA_0_9 + 0.5);
	if(Number>=1 && Number<=10)
	{
		Is_Test._1_10=1;
		return Number;
	}

	Number = (int)((Y-Is_Test.parameterB_10_13)/Is_Test.parameterA_10_13 + 0.5);
	if(Number>=11 && Number<=14)
	{
		Is_Test._11_14=1;
		return Number;
	}

	Number = (int)((Y-Is_Test.parameterB_14_23)/Is_Test.parameterA_14_23 + 0.5);
	if(Number>=15 && Number<=24)
	{
		Is_Test._15_24=1;
		return Number;
	}

	Number = (int)((Y-Is_Test.parameterB_24_31)/Is_Test.parameterA_24_31 + 0.5);
	if(Number>=25 && Number<=32)
	{
		Is_Test._25_32=1;
		return Number;
	}

	Number = (int)((Y-Is_Test.parameterB_32_34)/Is_Test.parameterA_32_34 + 0.5);
	if(Number>=33 && Number<=35)
	{
		Is_Test._33_35=1;
		return Number;
	}

	// Number = (int)((Y-Is_Test.parameterB_33_35)/Is_Test.parameterA_33_35 + 0.5);
	// if(Number>=34 && Number<=37)
	// {
	// 	return Number;
	// }

	// Number = (int)((Y-Is_Test.parameterB_36_42)/Is_Test.parameterA_36_42 + 0.5);
	// if(Number>=38 && Number<=43)
	// {
	// 	return Number;
	// }
	
	
	return Number;
}

